import axios from 'axios'
import md5 from 'js-md5'

const service = axios.create({
  baseURL: '', // api 的 base_url
  timeout: 300000, // request timeout
  withCredentials: true
})

/* eslint-disable no-proto */
service.__proto__ = axios
/* eslint-enable */

// request interceptor
service.interceptors.request.use(
  config => {
    var time = new Date().getTime()
    config.headers.common = {
      'x-Access-time': time,
      'x-Access-token': md5(time + 'cspg'),
    }
    console.info('请求拦截', config)
    return config
  },
  error => {
    Promise.reject(error)
  }
)

// response interceptor 401未授权 403权限拒绝
service.interceptors.response.use(
  response => {
    console.info('正常响应拦截', response)
    return response
  },
  error => {
    console.info('错误响应拦截：', error.response)
    return Promise.reject(error)
  }
)

export default service
